﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Text.RegularExpressions;
using UrlRouter.Web;
using System.Web;
using System.Security.Permissions;
using System.Collections.Specialized;

namespace UrlRouter.Configuration
{
    /// <summary>
    /// 从config读取单条的规则设置
    /// </summary>
    public class RewriteSettings : ConfigurationElement
    {
        public RewriteSettings() : this((string)null)
        {

        }
        public RewriteSettings(string elementName) 
        {
            
        }
        /// <summary>
        /// 单独一条规则,使用的驱动名称, 默认为父设置
        /// </summary>
        [ConfigurationProperty("provider", IsRequired = false)]
        public string Provider
        {
            get
            {
                return (string)base["provider"];
            }
            set
            {
                base["ruleProvider"] = value;
            }
        }
        /// <summary>
        /// 必须唯一的规则ID名称
        /// </summary>
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return (string)base["name"];
            }
            set
            {
                base["name"] = value;
            }
        }
        /// <summary>
        /// 设置转向的参数默认为none
        /// </summary>
        [ConfigurationProperty("redirect", IsRequired = false, DefaultValue = RedirectOption.None)]
        public RedirectOption Redirect
        {
            get
            {
                return (RedirectOption)base["redirect"];
            }
            set
            {
                base["redirect"] = value;
            }
        }
        /// <summary>
        /// 设置重写的参数,默认为Application:程序内部. 可以设置Doman为域名之间
        /// </summary>
        [ConfigurationProperty("rewrite", IsRequired = false, DefaultValue = RewriteOption.Application)]
        public RewriteOption Rewrite
        {
            get
            {
                return (RewriteOption)base["rewrite"];
            }
            set
            {
                base["rewrite"] = value;
            }
        }
        /// <summary>
        /// 转向的模式 分为301永久,和302临时,将写入http头代码状态中.
        /// </summary>
        [ConfigurationProperty("redirectMode", IsRequired = false, DefaultValue = RedirectModeOption.Temporary)]
        public RedirectModeOption RedirectMode
        {
            get
            {
                return (RedirectModeOption)base["redirectMode"];
            }
            set
            {
                base["redirectMode"] = value;
            }
        }
        /// <summary>
        /// 重写URL的参数类型处理, 默认为执行来自客户端的查询参数
        /// </summary>
        [ConfigurationProperty("rewriteUrlParameter", DefaultValue = RewriteUrlParameterOption.ExcludeFromClientQueryString)]
        public RewriteUrlParameterOption RewriteUrlParameter
        {
            get
            {
                return (RewriteUrlParameterOption)base["rewriteUrlParameter"];
            }
            set
            {
                base["rewriteUrlParameter"] = value;
            }
        }
        /// <summary>
        /// 忽略大小写,默认不忽略
        /// </summary>
        [ConfigurationProperty("ignoreCase", IsRequired = false, DefaultValue = false)]
        public bool IgnoreCase
        {
            get
            {
                return (bool)base["ignoreCase"];
            }
            set
            {
                base["ignoreCase"] = value;
            }
        }



        private NameValueCollection attributes = new NameValueCollection();

        /// <summary>
        /// 规则属性
        /// </summary>
        public NameValueCollection Attributes
        {
            get
            {
                return attributes;
            }
        }
        /// <summary>
        /// 从属性中返回string
        /// </summary>
        /// <param name="name"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public string GetAttribute(string name, string defaultValue)
        {
            if (attributes[name] == null)
                return defaultValue;
            else
                return attributes[name];
        }
        /// <summary>
        /// 从属性中返回int类型
        /// </summary>
        /// <param name="name"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public int GetInt32Attribute(string name, int defaultValue)
        {
            if (attributes[name] == null)
                return defaultValue;
            else
                return Convert.ToInt32(attributes[name]);
        }
        /// <summary>
        /// 从属性中返回bool类型
        /// </summary>
        /// <param name="name"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public bool GetBooleanAttribute(string name, bool defaultValue)
        {
            if (attributes[name] == null)
                return defaultValue;
            else
                return bool.Parse(attributes[name]);
        }
        /// <summary>
        /// 从属性中返回一个泛型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetEnumAttribute<T>(string name, T defaultValue)
        {
            if (attributes[name] == null)
                return defaultValue;
            else
                return (T) Enum.Parse(typeof(T), attributes[name]);
        }
        /// <summary>
        /// 覆盖方法,添加属性名和值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
        {
            attributes.Add(name, value);
            return true;
        }

    }
}
